package com.splic.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.Security;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @ClassName EmailUtil
 * @Description TODO
 * @Author yuxiangjie
 * @Date 2021/1/29 11:57
 * @Version 1.0
 */
public class EmailUtil {
    protected static Logger logger = LoggerFactory.getLogger(EmailUtil.class);

        private static final String SMTP_SERVER = "smtp.unimarketing.org";

        /**
         * SMTP协议发送邮件 参数（阿里云）
         */
        private static final String APIKey = "noreply@easyjoy.me";
        private static final String APISecret = "EasyJoy5567831!";
        private static final String AccountName = "EasyJoy Technologies";

        /**
         * 阿里云邮件发送 参数
         */
        private static final String AccessKeyId = "LTAI4G3YTWfwaGkiYp32d7sQ";
        private static final String AccessKeySecret = "wVIKfsNQhn2ZlkT2d0oSI1Lt4Bv9YN";

        public static String sendEmailCode(String account,String subject,String content) throws Exception {
              return sendEmailCodeBySmtp(account,subject,content);
        }

        public static String sendEmailCodeBySmtp(String account,String subject,String content) throws Exception {
            //设置SSL连接、邮件环境
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

            Properties properties = new Properties();
            properties.put("mail.smtp.host", "mail.nicematrix.com");
            properties.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            properties.put("mail.smtp.socketFactory.fallback", "false");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.port", "465");
            properties.put("mail.smtp.socketFactory.port", 465);
            properties.put("mail.transport.protocol", "smtp");
            properties.put("mail.smtp.ssl.checkserveridentity", true); // Compliant
            System.setProperty("https.protocols", "TLSv1.2");
            //建立邮件会话
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                //身份认证
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(APIKey,APISecret);
                }
            });

            //建立邮件对象
            MimeMessage message = new MimeMessage(session);
            //设置邮件的发件人、收件人、主题
            message.setFrom(new InternetAddress(APIKey, AccountName));
            message.setRecipients(javax.mail.Message.RecipientType.TO, account);
            message.setSubject(subject);
            //文本
            javax.mail.Multipart multipart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            // 设置HTML内容
            html.setContent(content, "text/html; charset=utf-8");
            multipart.addBodyPart(html);
            // 将MiniMultipart对象设置为邮件内容
            message.setContent(multipart);
            message.setSentDate(new Date());
            message.saveChanges();
            //发送邮件
            Transport.send(message);

            return "ok";
        }

}
